﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using ParkingLot.Utility.MessageHelp;

namespace ParkingLot.Utility.SocketHelp
{
    /*广播和组播的接收：接收端并不是直接从发送端接收消息，而是接收来自广播或组播组的消息
    单播：接收端直接从发送端接收消息*/


    /// <summary>
    /// UDP收包工厂 ---设计模式之简单工厂模式
    /// </summary>
    public class UDPReceiveFactory
    {

        /// <summary>
        /// 创建组播对象
        /// </summary>
        /// <param name="localPort">源端口</param>
        /// <param name="receivePort">目的端口</param>
        /// <returns></returns>
        public static UDPReceiveHelp CreateGroupReceive(int localPort, IPEndPoint receivEndPoint, List<IPAddress> joinMulticastList = null)
        {
           
            //初始化组播需要端口号
            UdpClient udpClient = new UdpClient(localPort);
            //加入组播
            if (joinMulticastList != null)
            {
                foreach (var item in joinMulticastList)
                {
                    udpClient.JoinMulticastGroup(item);
                }
            }
            udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            udpClient.Client.Bind(receivEndPoint);
            //自发自收
            udpClient.EnableBroadcast = true;
            //组播或者广播
            udpClient.MulticastLoopback = true;

            UDPReceiveHelp udpReceiveHelp = new UDPReceiveHelp(udpClient, receivEndPoint);
            //组播需要添加组播列表
            return udpReceiveHelp;
        }

        /// <summary>
        /// 创建广播对象
        /// </summary>
        /// <param name="localPort">本地端口号</param>
        /// <param name="receivEndPoint">目的网络端点</param>
        /// <param name="joinMulticastList">组播的组网</param>
        /// <returns></returns>
        public static UDPReceiveHelp CreateAirringReceive(int localPort, IPEndPoint receivEndPoint, List<IPAddress> joinMulticastList = null)
        {
            //广播 广播不需要端口号
            UdpClient udpClient = new UdpClient();
            udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            udpClient.Client.Bind(receivEndPoint);
            //自发自收
            udpClient.EnableBroadcast = true;
            //组播
            udpClient.MulticastLoopback = true;
            if (joinMulticastList != null)
            {
                foreach (var item in joinMulticastList)
                {
                    udpClient.JoinMulticastGroup(item);
                }
            }
           

            UDPReceiveHelp udpReceiveHelp = new UDPReceiveHelp(udpClient, receivEndPoint);
            //组播需要添加组播列表
            return udpReceiveHelp;
        }

        /// <summary>
        /// 创建单播对象
        /// </summary>
        /// <param name="localPort">本地端口号</param>
        /// <param name="receivEndPoint">目的网络端点</param>
        /// <param name="joinMulticastList">组播的组网</param>
        /// <returns></returns>
        public static UDPReceiveHelp CreateUniReceive(IPEndPoint localEndPoint)
        {
            UdpClient udpClient = new UdpClient(localEndPoint);
            IPEndPoint receivEndPoint=new IPEndPoint(IPAddress.Parse(MessageManage.Instance.ClientToServerIP), MessageManage.Instance.ClientToServerPort);
            //自发自收
            udpClient.EnableBroadcast = true;
            //组播
            udpClient.MulticastLoopback = true;


            return new UDPReceiveHelp(udpClient, receivEndPoint);
        }




    }
}
